#include "RemoveBookmarksCommand.h"

#include "BookmarksManager.h"
#include "BookmarkNode.h"

RemoveBookmarksCommand::RemoveBookmarksCommand(BookmarksManager *bookmarkManager, BookmarkNode *parent, int row)
    : QUndoCommand(BookmarksManager::tr("Remove Bookmark"))
    , m_row(row)
    , m_bookmarkManager(bookmarkManager)
    , m_node(parent->children().value(row))
    , m_parent(parent)
    , m_done(false)
{
}

RemoveBookmarksCommand::~RemoveBookmarksCommand()
{
    if (m_done && !m_node->parent())
        delete m_node;
}

void RemoveBookmarksCommand::undo()
{
    m_parent->add(m_node, m_row);
    emit m_bookmarkManager->entryAdded(m_node);
    m_done = false;
}

void RemoveBookmarksCommand::redo()
{
    m_parent->remove(m_node);
    emit m_bookmarkManager->entryRemoved(m_parent, m_row, m_node);
    m_done = true;
}
